package 二叉树相关问题.中度题;

public class 从根节点到叶子节点的数字之和 {
    public int sumNumbers(TreeNode root) {
        return dfs(root,0);
    }

    private int dfs(TreeNode root, int preSum) {
        if (root==null)
            return 0;
        int sum=10*preSum+root.val;
        if (root.left==null && root.right==null)
            return sum;
        return dfs(root.left,sum)+dfs(root.right,sum);
    }
}
